<?php

class PanelModel extends Model {
	function FileDisplay($dirpath) {
		$arrayfile = array();
		$iterator = 0;
		// "M://Kuliah/Semester6/progin/tugas/5_SOCKETPROGRAMMING/workdirserver"
		$TrackDir = opendir($dirpath);
		while (($file = readdir($TrackDir)) == true) {
			if ($file == "." || $file == "..") {
			}
			else {
				$path_parts = pathinfo($file);
				$type = filetype($dirpath ."/" .$path_parts['basename']);
				if($type!="dir"){
					$arrayfile[$iterator]['name'] = $path_parts['basename'];
					$arrayfile[$iterator]['type'] = $path_parts['extension'];
					$arrayfile[$iterator]['size'] = filesize($dirpath ."/" .$path_parts['basename']);
					$arrayfile[$iterator]['lastmodified'] = date ("F d Y H:i:s",filemtime($dirpath ."/" .$path_parts['basename']));
					$arrayfile[$iterator]['pathdownload'] = BASE_PATH ."/".$dirpath ."/" .$path_parts['basename'];
					$iterator++;
				}
			}
		}
		closedir($TrackDir);
		return $arrayfile;
	} 
	function DirDisplay($dirpath) {
		$arrayfile = array();
		$iterator = 0;
		$TrackDir = opendir($dirpath);
		while (($file = readdir($TrackDir)) == true) {
			if ($file == "." || $file == "..") {
			}
			else {
				$path_parts = pathinfo($file);
				$type = filetype($dirpath ."/" .$path_parts['basename']);
				if($type=="dir"){
					$arrayfile[$iterator]['name'] = $path_parts['basename'];
					$arrayfile[$iterator]['size'] = $this->calc_dir_size($dirpath ."/" .$path_parts['basename']);
					$arrayfile[$iterator]['lastmodified'] = date ("F d Y H:i:s",filemtime($dirpath ."/" .$path_parts['basename']));
				}
				$iterator++;
			}
		}
		closedir($TrackDir);
		return $arrayfile;
	}
	
	function calc_dir_size($path)
	{
		/**
		 * Calculate directory size.
		 * @param string $path without trailing '/' or '\' (eg. '/users/sampleUser', not '/users/sampleUser/')
		 * @return int size in bytes
		 */
		$size = 0;		 
		if ($handle = opendir($path))
		{
			while (false !== ($entry = readdir($handle)))
			{
				$current_path = $path . '/' . $entry;
				if ($entry != '.' && $entry != '..' && !is_link($current_path))
				{
					if (is_file($current_path))
						$size += filesize($current_path);
					elseif (is_dir($current_path))
						$size = calc_dir_size($current_path);
				}
			}
		}
		closedir($handle);
		return $size;
	}
}
